import config
from PySide6.QtWidgets import  QDialog, QVBoxLayout, QLabel, QLineEdit, QPushButton, QFormLayout

class MainConfigDialog(QDialog):
    def __init__(self):
        super().__init__()
        
        self.setWindowTitle("配置")
        self.setFixedSize(500, 300)

        # 输入框
        self.wc_url_input = QLineEdit(self, text=config.read('wc_url'))
        self.wc_key_input = QLineEdit(self, text=config.read('wc_key'))
        self.wc_secret_input = QLineEdit(self, text=config.read('wc_secret'))
        self.update_time_input = QLineEdit(self, text=config.read('update_time'))

        # 表单
        self.form = QFormLayout()
        self.form.addRow(QLabel("WooCommerce 网址"), self.wc_url_input)
        self.form.addRow(QLabel("WooCommerce Key"), self.wc_key_input)
        self.form.addRow(QLabel("WooCommerce Secret"), self.wc_secret_input)
        self.form.addRow(QLabel("商品同步间隔(秒)"), self.update_time_input)

        self.button = QPushButton("保存")
        self.button.clicked.connect(self.save)
        
        # 布局
        layout = QVBoxLayout()
        layout.addLayout(self.form)
        layout.addWidget(self.button)
        
        self.setLayout(layout)
    
    def save(self):
        config.write('wc_url', self.wc_url_input.text())
        config.write('wc_key', self.wc_key_input.text())
        config.write('wc_secret', self.wc_secret_input.text())
        config.write('update_time', self.update_time_input.text())
        self.accept()
    